package com.xiyueyezi.xiyueyezi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.xiyueyezi.xiyueyezi.homeFragment.BlankFragment1;
import com.xiyueyezi.xiyueyezi.homeFragment.BlankFragment2;
import com.xiyueyezi.xiyueyezi.homeFragment.BlankFragment3;
import com.xiyueyezi.xiyueyezi.test.UsbTestActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Fragment fragment1, fragment2, fragment3;
    private ImageView iv_home, iv_find, iv_mine;
    private LinearLayout ll_content;

    private Fragment currentFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        if (!isAgree()) {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        } else {
            Log.d(TAG, "onCreate: " + isLogin());
            if (isLogin()) {
                initView();
            } else {
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
            }
        }

    }

    private boolean isAgree() {
        //检查是否已同意隐私政策
        SharedPreferences sp = getSharedPreferences("xiyueyezi", MODE_PRIVATE);
        return sp.getBoolean("isPrivacyPolicyAccepted", false);
    }

    private boolean isLogin() {
        //检查是否已登录
//        SharedPreferences sp = getSharedPreferences("xiyueyezi", MODE_PRIVATE);
//        return sp.getBoolean("isLogin", false);
        return true;

    }

    private void initView() {
        //
        iv_home = findViewById(R.id.iv_Home);
        iv_find = findViewById(R.id.iv_Find);
        iv_mine = findViewById(R.id.iv_Mine);
        iv_home.setOnClickListener(v -> {
            onHomeClive();
        });
        iv_find.setOnClickListener(v -> {
            onFindClick();
        });
        iv_mine.setOnClickListener(v -> {
            onMineClick();
        });
        //
        ll_content = findViewById(R.id.ll_content);

        //初始化底部标签栏
        initTab();

    }

    private void initTab() {

        if (fragment1 == null) {
            fragment1 = new BlankFragment1();
        }

        iv_home.setImageResource(R.drawable.red_button);

        //默认显示第一个
        if (!fragment1.isAdded()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.ll_content, fragment1)
                    .commit();
        }
        currentFragment = fragment1;
    }

//    public void onButton1(View view) {
//        Intent intent = new Intent(this, UsbTestActivity.class);
//        startActivity(intent);
//        finish();
//    }


    /**
     * 添加或显示碎片
     *
     * @param transaction
     * @param fragment
     */
    private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
        if (currentFragment == fragment) {
            return;
        }
        //如果当前fragment未被添加则添加到Fragment
        if (!fragment.isAdded()) {
            Log.d(TAG, "addOrShowFragment: 添加");
            transaction
                    .hide(currentFragment)
                    .add(R.id.ll_content, fragment)
                    .commit();
        } else {
            Log.d(TAG, "addOrShowFragment: 切换");
            transaction
                    .hide(currentFragment)
                    .show(fragment)
                    .commit();
        }
        currentFragment = fragment;
    }


    public void onHomeClive() {
        if (fragment1 == null) {
            fragment1 = new BlankFragment1();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), fragment1);
        //底部变化
        iv_home.setImageResource(R.drawable.red_button);
        iv_find.setImageResource(R.drawable.one_button);
        iv_mine.setImageResource(R.drawable.one_button);
    }

    public void onFindClick() {
        if (fragment2 == null) {
            fragment2 = new BlankFragment2();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), fragment2);
        //底部变化
        iv_home.setImageResource(R.drawable.one_button);
        iv_find.setImageResource(R.drawable.red_button);
        iv_mine.setImageResource(R.drawable.one_button);
    }

    public void onMineClick() {
        if (fragment3 == null) {
            fragment3 = new BlankFragment3();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(), fragment3);
        //底部变化
        iv_home.setImageResource(R.drawable.one_button);
        iv_find.setImageResource(R.drawable.one_button);
        iv_mine.setImageResource(R.drawable.red_button);
    }
}